package main

import (
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main()  {
	c := make(chan os.Signal)

	signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGUSR1, syscall.SIGUSR2)
	go func() {
		for s := range c {
			switch s {
			case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
				log.Println("退出")
				ExitFunc()
			case syscall.SIGUSR1:
				log.Println("user1", s)
			case syscall.SIGUSR2:
				log.Println("user2", s)
			default:
				log.Println("other", s)
			}
		}
	}()

	log.Println("进程启动......")
	sum := 0
	for {
		sum++
		log.Println("sum: ", sum)
		time.Sleep(time.Second)
	}
}

func ExitFunc()  {
	log.Println("开始退出......")
	log.Println("执行清理......")
	log.Println("结束退出......")
	os.Exit(0)
}

func main1()  {
	c := make(chan os.Signal)
	signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGUSR1, syscall.SIGUSR2)
	log.Println("启动")
	s := <- c
	log.Println("信号：", s)
}